Certified Java Programmer Mock Exam


Question 1


class Green {
  public static void main(String args[]) {
    short[] s = {1, -1, 3, 4};
    for (int i = 0; i<s.length;i++) {
      switch(s[i]) {
        case 2-1: System.out.print("v ");
        break;
        case 'd'-'a': System.out.print("w ");
        break;
        case 0: System.out.print("x ");
        break;
        case ~0: System.out.print("y ");
        break;
        case 4&5: System.out.print("z ");
        break;
        default: System.out.print("Default ");
      }
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: v w x y
b. Prints: v w x y z Default
c. Prints: v y w z
d. Prints: Default Default Default Default
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 2


class Blue {
  public static void main(String args[]) {
    long[] l = {1, -1, 3, 4};
    for (int i = 0; i<l.length;i++) {
      switch(l[i]) {
        case 2-1: System.out.print("v ");
        break;
        case 'd'-'a': System.out.print("w ");
        break;
        case 0: System.out.print("x ");
        break;
        case ~0: System.out.print("y ");
        break;
        case 4&5: System.out.print("z ");
        break;
        default: System.out.print("Default ");
      }
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: v w x y
b. Prints: v w x y z Default
c. Prints: v y w z
d. Prints: Default Default Default Default
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 3


class Black {
  public static void main (String args[]) {
    int i = 0;
    int j = 0;
label1:
    while (i++<5) {
label2:
      for (;;) {
label3:
        do {
          System.out.print(i + j);
          switch (i+j++) {
            case 0: continue label3;
            case 1: continue label2;
            case 2: continue label1;
            case 3: break;
            case 4: break label3;
            case 5: break label2;
            case 6: break label1;
            default: break label1;
          }
        } while (++j<5);
      }
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 12457
b. Prints: 02357
c. Prints: 02356
d. Prints: 1357
e. Prints: 1356
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 4


class Brown {
  public static void main (String args[]) {
    int i = 0;
    int j = 0;
label1:
    while (1) {
label2:
      for (;;i++) {
label3:
        do {
          System.out.print(i + j);
          switch (i+j++) {
            case 0: continue label3;
            case 1: continue label2;
            case 2: continue label1;
            case 3: break;
            case 4: break label3;
            case 5: break label2;
            case 6: break label1;
            default: break label1;
          }
        } while (++j<5);
      }
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1357
b. Prints: 02
c. Prints: 02356
d. Prints: 01346
e. Prints: 0236
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 5


class A {
  public static void main (String[] args) {
    int i = 0,j = 0,m = 0,n = 0,p;
label1:
    try {
      do {
        m++;
        try {
          p = i + j + m + n;
          if (p>=5) break label1;
          n++;
        } finally {i++;}
      } while (m++ < 2);
    } finally {j++;}
    System.out.print(i + "," + j + "," + m + "," + n);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1, 1, 2, 3
b. Prints: 1, 1, 2, 2
c. Prints: 1, 1, 3, 1
d. Prints: 1, 0, 3, 1
e. Prints: 2, 1, 3, 1
f. Prints: 2, 1, 4, 1
g. Prints: 0, 0, 3, 1
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 6


class Gray {
  public static void main (String args[]) {
    int i = 0;
    int j = 0;
    int k = 0;
label1:
    do {
      k++;
label2:
      do {
        j++;
label3:
        do {
          i++;
          System.out.print((i + j + k + ", "));
          switch ((i+j+k)) {
            case 3: continue label3;
            case 4: continue label2;
            case 5: continue label1;
            case 6: break;
            case 7: break label3;
            case 8: break label2;
            default: break label1;
	      }
        } while (i++ < 2);
      } while (j++ < 2);
    } while (k++ < 2);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 3, 4, 6, 8, 12
b. Prints: 3, 4, 7, 10
c. Prints: 3, 5,
d. Prints: 3, 5, 9,
e. Prints: 3, 6, 8, 12
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 7


class Gray {
  public static void main (String args[]) {
    int i = 0;
    int j = 0;
    int k = 0;
label1:
    do {
      k++;
label2:
      do {
        j++;
label3:
        do {
          i++;
          System.out.print((i + j + k + ", "));
          switch ((i+j+k)) {
            default: break label1;
            case 3: break label3;
            case 4: break label3;
            case 5: break label2;
            case 6: continue label3;
            case 7: continue label2;
            case 8: continue label2;
            case 9: continue label1;
	      }
        } while (i++ < 2);
      } while (j++ < 2);
    } while (k++ < 2);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 3, 4, 6, 12
b. Prints: 3, 4, 7, 10
c. Prints: 3, 5, 8, 12,
d. Prints: 3, 6, 12,
e. Prints: 3, 6, 8, 12,
f. Runtime Exception
g. Compiler Error
h. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1